//
//  HomeView.swift
//  HomeView
//
//  Created by Ben on 2021/8/13.
//

import SwiftUI

struct HomeView: View {
    
    @EnvironmentObject var customTabbarVM: CustomTabbarViewModel
    @StateObject var locationManager = LocationManager()
    @EnvironmentObject var homeVM: HomeViewModel
    @EnvironmentObject var userManagerVM: UserManagerViewModel
    init() {
        
    }
    
    
    var body: some View {
        
        NavigationView {
            // 自定义导航头
            GeometryReader { proxy in
                
                ZStack(alignment: .bottom) {
                    
                    VStack {
                        HomeNavView()
                            .environmentObject(locationManager)
                            .environmentObject(homeVM)
                            .frame(maxWidth: .infinity)
                            .padding(.horizontal, 16)
                            

                        HomeTabView()
                            .environmentObject(homeVM)

                    }
                    
                    if userManagerVM.token == "" {
                        HomeNoLoginPopView()
                    }
                }
                .frame(maxHeight: .infinity, alignment: .top)
                .onAppear {
                    withAnimation {
                        customTabbarVM.atFront = true
                    }
                    locationManager.manager.requestLocation()
                }
                .padding(.bottom, 50)
                
            }
            .navigationTitle("")
            .navigationBarHidden(true)
        }
        .task {
            await homeVM.getSuperGoodsCategoryList()
        }


    }
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        
        HomeView()
    }
}

